{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic = pd.read_csv('./titanic.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Braund, Mr. Owen Harris</td>\n",
       "      <td>male</td>\n",
       "      <td>22.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>A/5 21171</td>\n",
       "      <td>7.2500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
       "      <td>female</td>\n",
       "      <td>38.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>PC 17599</td>\n",
       "      <td>71.2833</td>\n",
       "      <td>C85</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Heikkinen, Miss. Laina</td>\n",
       "      <td>female</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>STON/O2. 3101282</td>\n",
       "      <td>7.9250</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
       "      <td>female</td>\n",
       "      <td>35.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>113803</td>\n",
       "      <td>53.1000</td>\n",
       "      <td>C123</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Allen, Mr. William Henry</td>\n",
       "      <td>male</td>\n",
       "      <td>35.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>373450</td>\n",
       "      <td>8.0500</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Moran, Mr. James</td>\n",
       "      <td>male</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>330877</td>\n",
       "      <td>8.4583</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Q</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>McCarthy, Mr. Timothy J</td>\n",
       "      <td>male</td>\n",
       "      <td>54.0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>17463</td>\n",
       "      <td>51.8625</td>\n",
       "      <td>E46</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>Palsson, Master. Gosta Leonard</td>\n",
       "      <td>male</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>349909</td>\n",
       "      <td>21.0750</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)</td>\n",
       "      <td>female</td>\n",
       "      <td>27.0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>347742</td>\n",
       "      <td>11.1333</td>\n",
       "      <td>NaN</td>\n",
       "      <td>S</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>Nasser, Mrs. Nicholas (Adele Achem)</td>\n",
       "      <td>female</td>\n",
       "      <td>14.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>237736</td>\n",
       "      <td>30.0708</td>\n",
       "      <td>NaN</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   PassengerId  Survived  Pclass  \\\n",
       "0            1         0       3   \n",
       "1            2         1       1   \n",
       "2            3         1       3   \n",
       "3            4         1       1   \n",
       "4            5         0       3   \n",
       "5            6         0       3   \n",
       "6            7         0       1   \n",
       "7            8         0       3   \n",
       "8            9         1       3   \n",
       "9           10         1       2   \n",
       "\n",
       "                                                Name     Sex   Age  SibSp  \\\n",
       "0                            Braund, Mr. Owen Harris    male  22.0      1   \n",
       "1  Cumings, Mrs. John Bradley (Florence Briggs Th...  female  38.0      1   \n",
       "2                             Heikkinen, Miss. Laina  female  26.0      0   \n",
       "3       Futrelle, Mrs. Jacques Heath (Lily May Peel)  female  35.0      1   \n",
       "4                           Allen, Mr. William Henry    male  35.0      0   \n",
       "5                                   Moran, Mr. James    male   NaN      0   \n",
       "6                            McCarthy, Mr. Timothy J    male  54.0      0   \n",
       "7                     Palsson, Master. Gosta Leonard    male   2.0      3   \n",
       "8  Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)  female  27.0      0   \n",
       "9                Nasser, Mrs. Nicholas (Adele Achem)  female  14.0      1   \n",
       "\n",
       "   Parch            Ticket     Fare Cabin Embarked  \n",
       "0      0         A/5 21171   7.2500   NaN        S  \n",
       "1      0          PC 17599  71.2833   C85        C  \n",
       "2      0  STON/O2. 3101282   7.9250   NaN        S  \n",
       "3      0            113803  53.1000  C123        S  \n",
       "4      0            373450   8.0500   NaN        S  \n",
       "5      0            330877   8.4583   NaN        Q  \n",
       "6      0             17463  51.8625   E46        S  \n",
       "7      1            349909  21.0750   NaN        S  \n",
       "8      2            347742  11.1333   NaN        S  \n",
       "9      0            237736  30.0708   NaN        C  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 问题1：按性别为数据分组？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'female': Int64Index([  1,   2,   3,   8,   9,  10,  11,  14,  15,  18,\n",
       "             ...\n",
       "             866, 871, 874, 875, 879, 880, 882, 885, 887, 888],\n",
       "            dtype='int64', length=314),\n",
       " 'male': Int64Index([  0,   4,   5,   6,   7,  12,  13,  16,  17,  20,\n",
       "             ...\n",
       "             873, 876, 877, 878, 881, 883, 884, 886, 889, 890],\n",
       "            dtype='int64', length=577)}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sex_group = titanic.groupby(titanic.Sex)\n",
    "sex_group.groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PassengerId</th>\n",
       "      <th>Survived</th>\n",
       "      <th>Pclass</th>\n",
       "      <th>Name</th>\n",
       "      <th>Age</th>\n",
       "      <th>SibSp</th>\n",
       "      <th>Parch</th>\n",
       "      <th>Ticket</th>\n",
       "      <th>Fare</th>\n",
       "      <th>Cabin</th>\n",
       "      <th>Embarked</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sex</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>female</th>\n",
       "      <td>314</td>\n",
       "      <td>314</td>\n",
       "      <td>314</td>\n",
       "      <td>314</td>\n",
       "      <td>261</td>\n",
       "      <td>314</td>\n",
       "      <td>314</td>\n",
       "      <td>314</td>\n",
       "      <td>314</td>\n",
       "      <td>97</td>\n",
       "      <td>312</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>male</th>\n",
       "      <td>577</td>\n",
       "      <td>577</td>\n",
       "      <td>577</td>\n",
       "      <td>577</td>\n",
       "      <td>453</td>\n",
       "      <td>577</td>\n",
       "      <td>577</td>\n",
       "      <td>577</td>\n",
       "      <td>577</td>\n",
       "      <td>107</td>\n",
       "      <td>577</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        PassengerId  Survived  Pclass  Name  Age  SibSp  Parch  Ticket  Fare  \\\n",
       "Sex                                                                            \n",
       "female          314       314     314   314  261    314    314     314   314   \n",
       "male            577       577     577   577  453    577    577     577   577   \n",
       "\n",
       "        Cabin  Embarked  \n",
       "Sex                      \n",
       "female     97       312  \n",
       "male      107       577  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sex_group.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Sex\n",
       "female    233\n",
       "male      109\n",
       "Name: Survived, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sex_group.Survived.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Survived</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sex</th>\n",
       "      <th>Survived</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">female</th>\n",
       "      <th>0</th>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">male</th>\n",
       "      <th>0</th>\n",
       "      <td>468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>109</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 Survived\n",
       "Sex    Survived          \n",
       "female 0               81\n",
       "       1              233\n",
       "male   0              468\n",
       "       1              109"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.groupby([titanic.Sex, titanic.Survived])[['Survived']].count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 问题2：如何同时对性别与年龄分组？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PassengerId    290\n",
       "Survived       290\n",
       "Pclass         290\n",
       "Name           290\n",
       "Sex            290\n",
       "Age            290\n",
       "SibSp          290\n",
       "Parch          290\n",
       "Ticket         290\n",
       "Fare           290\n",
       "Cabin          125\n",
       "Embarked       288\n",
       "dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic[(titanic.Survived == 1) & titanic.Age.notna()].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "sex_age_group = titanic.groupby([titanic.Age, titanic.Sex])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Age    Sex   \n",
       "0.42   male      1\n",
       "0.67   male      1\n",
       "0.75   female    2\n",
       "0.83   male      2\n",
       "0.92   male      1\n",
       "1.00   female    2\n",
       "       male      3\n",
       "2.00   female    2\n",
       "       male      1\n",
       "3.00   female    1\n",
       "       male      4\n",
       "4.00   female    5\n",
       "       male      2\n",
       "5.00   female    4\n",
       "6.00   female    1\n",
       "       male      1\n",
       "7.00   female    1\n",
       "       male      0\n",
       "8.00   female    1\n",
       "       male      1\n",
       "9.00   female    0\n",
       "       male      2\n",
       "10.00  female    0\n",
       "       male      0\n",
       "11.00  female    0\n",
       "       male      1\n",
       "12.00  male      1\n",
       "13.00  female    2\n",
       "14.00  female    3\n",
       "       male      0\n",
       "                ..\n",
       "51.00  male      1\n",
       "52.00  female    2\n",
       "       male      1\n",
       "53.00  female    1\n",
       "54.00  female    3\n",
       "       male      0\n",
       "55.00  female    1\n",
       "       male      0\n",
       "55.50  male      0\n",
       "56.00  female    1\n",
       "       male      1\n",
       "57.00  female    0\n",
       "       male      0\n",
       "58.00  female    3\n",
       "       male      0\n",
       "59.00  male      0\n",
       "60.00  female    1\n",
       "       male      1\n",
       "61.00  male      0\n",
       "62.00  female    1\n",
       "       male      1\n",
       "63.00  female    2\n",
       "64.00  male      0\n",
       "65.00  male      0\n",
       "66.00  male      0\n",
       "70.00  male      0\n",
       "70.50  male      0\n",
       "71.00  male      0\n",
       "74.00  male      0\n",
       "80.00  male      1\n",
       "Name: Survived, Length: 145, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sex_age_group.Survived.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(Interval(20.0, 30.0, closed='right'),\n",
       "  'male'): Int64Index([  0,  23,  34,  37,  51,  57,  60,  69,  72,  73,\n",
       "             ...\n",
       "             833, 836, 848, 861, 864, 870, 883, 884, 886, 889],\n",
       "            dtype='int64', length=149),\n",
       " (Interval(30.0, 40.0, closed='right'),\n",
       "  'female'): Int64Index([  1,   3,  18,  25,  40,  61,  85,  98, 123, 161, 190, 211, 215,\n",
       "             218, 230, 258, 269, 279, 318, 319, 325, 327, 328, 346, 357, 383,\n",
       "             387, 396, 412, 416, 472, 486, 503, 506, 516, 518, 540, 558, 559,\n",
       "             576, 577, 581, 609, 610, 657, 670, 716, 759, 763, 767, 797, 801,\n",
       "             809, 835, 885],\n",
       "            dtype='int64'),\n",
       " (Interval(20.0, 30.0, closed='right'),\n",
       "  'female'): Int64Index([  2,   8,  41,  53,  56,  66,  79,  88, 100, 106, 133, 141, 142,\n",
       "             151, 199, 216, 246, 247, 251, 255, 257, 289, 290, 293, 309, 310,\n",
       "             312, 315, 316, 322, 323, 341, 345, 356, 369, 376, 393, 394, 399,\n",
       "             402, 423, 426, 436, 437, 443, 473, 474, 498, 501, 520, 534, 537,\n",
       "             539, 554, 567, 580, 600, 608, 615, 617, 627, 635, 641, 649, 708,\n",
       "             710, 717, 726, 729, 730, 742, 747, 799, 816, 823, 842, 858, 866,\n",
       "             874, 880, 882],\n",
       "            dtype='int64'),\n",
       " (Interval(30.0, 40.0, closed='right'),\n",
       "  'male'): Int64Index([  4,  13,  20,  21,  30,  70,  74,  99, 103, 104, 108, 122, 130,\n",
       "             137, 148, 179, 188, 189, 202, 206, 209, 224, 239, 248, 263, 265,\n",
       "             273, 285, 292, 332, 344, 360, 363, 382, 390, 400, 405, 429, 439,\n",
       "             447, 450, 461, 465, 471, 476, 512, 519, 528, 543, 548, 561, 569,\n",
       "             572, 579, 583, 590, 594, 595, 604, 605, 614, 616, 632, 636, 637,\n",
       "             661, 663, 665, 671, 673, 679, 690, 701, 705, 719, 722, 737, 741,\n",
       "             744, 749, 752, 758, 769, 795, 800, 805, 806, 808, 811, 812, 814,\n",
       "             817, 822, 838, 843, 847, 867, 872, 881, 890],\n",
       "            dtype='int64'),\n",
       " (nan, 'male'): Int64Index([  5,  17,  26,  29,  36,  42,  45,  46,  48,  55,\n",
       "             ...\n",
       "             825, 826, 828, 832, 837, 839, 846, 859, 868, 878],\n",
       "            dtype='int64', length=124),\n",
       " (Interval(50.0, 60.0, closed='right'),\n",
       "  'male'): Int64Index([  6,  94, 124, 150, 152, 155, 174, 222, 232, 249, 262, 317, 406,\n",
       "             449, 467, 487, 492, 582, 587, 626, 631, 647, 659, 684, 694, 695,\n",
       "             714, 857],\n",
       "            dtype='int64'),\n",
       " (Interval(0.0, 10.0, closed='right'),\n",
       "  'male'): Int64Index([  7,  16,  50,  63,  78, 164, 165, 171, 182, 183, 193, 261, 278,\n",
       "             305, 340, 348, 386, 407, 445, 480, 489, 549, 751, 755, 787, 788,\n",
       "             803, 819, 824, 827, 831, 850, 869],\n",
       "            dtype='int64'),\n",
       " (Interval(10.0, 20.0, closed='right'),\n",
       "  'female'): Int64Index([  9,  14,  22,  38,  39,  44,  49,  68,  71,  84, 111, 113, 114,\n",
       "             136, 156, 192, 208, 291, 307, 311, 329, 389, 404, 417, 427, 435,\n",
       "             446, 504, 542, 546, 585, 651, 654, 677, 689, 700, 702, 780, 781,\n",
       "             786, 807, 830, 853, 855, 875, 887],\n",
       "            dtype='int64'),\n",
       " (Interval(0.0, 10.0, closed='right'),\n",
       "  'female'): Int64Index([ 10,  24,  43,  58, 119, 147, 172, 184, 205, 233, 237, 297, 374,\n",
       "             381, 419, 448, 469, 479, 530, 535, 541, 618, 634, 642, 644, 691,\n",
       "             720, 750, 777, 813, 852],\n",
       "            dtype='int64'),\n",
       " (Interval(50.0, 60.0, closed='right'),\n",
       "  'female'): Int64Index([11, 15, 195, 268, 366, 496, 513, 571, 591, 765, 772, 774, 820,\n",
       "             879],\n",
       "            dtype='int64'),\n",
       " (Interval(10.0, 20.0, closed='right'),\n",
       "  'male'): Int64Index([ 12,  27,  59,  67,  86,  91, 125, 131, 138, 143, 144, 145, 163,\n",
       "             175, 191, 204, 220, 226, 228, 238, 266, 282, 283, 302, 333, 352,\n",
       "             371, 372, 378, 379, 385, 424, 433, 441, 500, 505, 532, 550, 566,\n",
       "             574, 575, 622, 640, 646, 664, 675, 682, 683, 686, 687, 688, 715,\n",
       "             721, 725, 731, 746, 748, 757, 762, 764, 775, 791, 802, 834, 840,\n",
       "             841, 844, 876, 877],\n",
       "            dtype='int64'),\n",
       " (nan,\n",
       "  'female'): Int64Index([ 19,  28,  31,  32,  47,  82, 109, 128, 140, 166, 180, 186, 198,\n",
       "             229, 235, 240, 241, 256, 264, 274, 300, 303, 306, 330, 334, 347,\n",
       "             358, 359, 367, 368, 375, 409, 415, 431, 457, 485, 502, 533, 564,\n",
       "             573, 578, 593, 596, 612, 653, 669, 680, 697, 727, 792, 849, 863,\n",
       "             888],\n",
       "            dtype='int64'),\n",
       " (Interval(60.0, 70.0, closed='right'),\n",
       "  'male'): Int64Index([33, 54, 170, 252, 280, 326, 438, 456, 545, 555, 570, 625, 672,\n",
       "             745],\n",
       "            dtype='int64'),\n",
       " (Interval(40.0, 50.0, closed='right'),\n",
       "  'male'): Int64Index([ 35,  62,  92, 110, 129, 149, 153, 160, 187, 197, 203, 217, 236,\n",
       "             245, 288, 314, 331, 338, 339, 349, 397, 414, 434, 453, 460, 462,\n",
       "             463, 482, 515, 525, 536, 544, 586, 592, 597, 599, 603, 621, 645,\n",
       "             660, 662, 668, 696, 698, 699, 707, 712, 723, 761, 771, 789, 818,\n",
       "             845, 860, 873],\n",
       "            dtype='int64'),\n",
       " (Interval(40.0, 50.0, closed='right'),\n",
       "  'female'): Int64Index([ 52, 132, 167, 177, 194, 254, 259, 272, 276, 299, 337, 362, 380,\n",
       "             432, 440, 458, 523, 526, 556, 638, 678, 706, 736, 754, 779, 796,\n",
       "             854, 856, 862, 865, 871],\n",
       "            dtype='int64'),\n",
       " (Interval(70.0, 80.0, closed='right'),\n",
       "  'male'): Int64Index([96, 116, 493, 630, 851], dtype='int64'),\n",
       " (Interval(60.0, 70.0, closed='right'),\n",
       "  'female'): Int64Index([275, 483, 829], dtype='int64')}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bins = np.arange(0, 110, 10)\n",
    "age_bins = pd.cut(titanic.Age, bins)\n",
    "sex_age_group2 = titanic.groupby([age_bins, titanic.Sex])\n",
    "sex_age_group2.groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Age       Sex   \n",
       "(0, 10]   female    19\n",
       "          male      19\n",
       "(10, 20]  female    34\n",
       "          male      10\n",
       "(20, 30]  female    61\n",
       "          male      23\n",
       "(30, 40]  female    46\n",
       "          male      23\n",
       "(40, 50]  female    21\n",
       "          male      12\n",
       "(50, 60]  female    13\n",
       "          male       4\n",
       "(60, 70]  female     3\n",
       "          male       1\n",
       "(70, 80]  male       1\n",
       "Name: Survived, dtype: int64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sex_age_group2.Survived.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "sex_age_group3 = titanic.groupby"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(20.0, 'male'): Int64Index([  0,  12,  23,  34,  37,  51,  57,  60,  69,  72,\n",
       "             ...\n",
       "             840, 848, 861, 864, 870, 876, 883, 884, 886, 889],\n",
       "            dtype='int64', length=148),\n",
       " (nan, 'male'): Int64Index([524], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([527], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([531], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([533], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([538], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([547], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([552], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([557], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([560], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([522], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([563], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([568], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([573], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([578], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([584], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([589], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([593], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([596], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([598], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([601], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([564], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([602], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([517], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([507], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([420], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([425], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([428], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([431], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([444], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([451], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([454], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([457], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([459], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([511], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([464], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([468], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([470], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([475], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([481], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([485], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([490], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([495], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([497], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([502], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([466], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([611], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([612], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([613], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([768], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([773], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([776], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([778], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([783], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([790], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([792], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([793], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([815], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([766], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([825], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([828], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([832], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([837], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([839], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([846], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([849], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([859], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([863], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([868], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([826], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([760], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([740], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([739], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([629], dtype='int64'),\n",
       " (80.0, 'male'): Int64Index([630], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([633], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([639], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([643], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([648], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([650], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([653], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([656], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([667], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([669], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([674], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([680], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([692], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([697], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([709], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([711], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([718], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([727], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([732], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([738], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([415], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([878], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([413], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([410], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([47], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([48], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([55], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([64], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([65], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([76], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([77], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([82], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([87], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([46], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([95], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([101], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([107], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([109], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([121], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([126], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([128], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([140], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([154], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([158], dtype='int64'),\n",
       " (70.0, 'male'): Int64Index([96, 116, 493, 672, 745, 851], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([159], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([45], dtype='int64'),\n",
       " (20.0,\n",
       "  'female'): Int64Index([  2,   8,  41,  53,  56,  66,  88, 100, 106, 113, 133, 141, 142,\n",
       "             151, 199, 216, 246, 247, 251, 255, 289, 290, 293, 310, 312, 315,\n",
       "             316, 323, 341, 345, 356, 369, 376, 393, 394, 399, 402, 404, 423,\n",
       "             426, 436, 437, 443, 473, 474, 498, 501, 539, 554, 567, 580, 600,\n",
       "             608, 615, 617, 627, 635, 641, 649, 708, 710, 717, 729, 730, 742,\n",
       "             816, 823, 858, 866, 874, 880, 882],\n",
       "            dtype='int64'),\n",
       " (30.0,\n",
       "  'female'): Int64Index([  1,   3,  18,  25,  61,  79,  85,  98, 123, 190, 211, 215, 218,\n",
       "             230, 257, 258, 269, 279, 309, 318, 322, 325, 327, 328, 357, 383,\n",
       "             387, 396, 412, 416, 472, 486, 503, 506, 516, 518, 520, 534, 537,\n",
       "             540, 558, 559, 576, 577, 581, 610, 657, 716, 726, 747, 759, 763,\n",
       "             767, 797, 799, 801, 809, 835, 842, 885],\n",
       "            dtype='int64'),\n",
       " (30.0, 'male'): Int64Index([  4,  13,  20,  21,  70,  74,  99, 103, 104, 108,\n",
       "             ...\n",
       "             814, 817, 822, 838, 843, 847, 867, 872, 881, 890],\n",
       "            dtype='int64', length=107),\n",
       " (40.0,\n",
       "  'female'): Int64Index([ 40,  52, 132, 161, 167, 194, 254, 272, 276, 319, 337, 346, 362,\n",
       "             380, 432, 440, 523, 556, 609, 638, 670, 678, 706, 736, 754, 779,\n",
       "             796, 854, 856, 862, 865, 871],\n",
       "            dtype='int64'),\n",
       " (nan, 'male'): Int64Index([5], dtype='int64'),\n",
       " (0.0,\n",
       "  'female'): Int64Index([ 10,  24,  43,  58, 119, 147, 172, 184, 205, 233, 237, 297, 374,\n",
       "             381, 448, 469, 479, 530, 535, 541, 618, 634, 642, 644, 691, 720,\n",
       "             750, 777, 813, 852],\n",
       "            dtype='int64'),\n",
       " (0.0,\n",
       "  'male'): Int64Index([  7,  16,  50,  63,  78, 164, 165, 171, 182, 183, 193, 261, 278,\n",
       "             305, 340, 348, 386, 407, 445, 480, 489, 549, 751, 755, 787, 788,\n",
       "             803, 824, 827, 831, 850, 869],\n",
       "            dtype='int64'),\n",
       " (10.0,\n",
       "  'female'): Int64Index([  9,  14,  22,  38,  39,  44,  49,  68,  71,  84, 111, 114, 136,\n",
       "             156, 192, 208, 291, 307, 311, 329, 389, 417, 419, 427, 435, 446,\n",
       "             504, 542, 546, 585, 651, 654, 677, 689, 700, 702, 780, 781, 786,\n",
       "             807, 830, 853, 855, 875, 887],\n",
       "            dtype='int64'),\n",
       " (50.0,\n",
       "  'female'): Int64Index([ 11,  15, 177, 195, 259, 268, 299, 458, 496, 513, 526, 571, 591,\n",
       "             765, 772, 774, 820, 879],\n",
       "            dtype='int64'),\n",
       " (50.0,\n",
       "  'male'): Int64Index([  6,  94, 124, 150, 152, 155, 174, 222, 232, 249, 262, 317, 406,\n",
       "             434, 449, 467, 482, 487, 492, 544, 582, 626, 631, 647, 659, 660,\n",
       "             695, 714, 723, 857],\n",
       "            dtype='int64'),\n",
       " (nan, 'male'): Int64Index([42], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([17], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([26], dtype='int64'),\n",
       " (10.0,\n",
       "  'male'): Int64Index([ 27,  59,  67,  86, 125, 138, 143, 144, 145, 163, 175, 191, 204,\n",
       "             220, 226, 228, 238, 266, 282, 283, 302, 333, 352, 371, 372, 379,\n",
       "             385, 424, 433, 500, 505, 532, 550, 566, 574, 575, 646, 675, 683,\n",
       "             686, 687, 688, 715, 721, 731, 746, 748, 757, 764, 775, 791, 802,\n",
       "             819, 834, 841, 844, 877],\n",
       "            dtype='int64'),\n",
       " (nan, 'female'): Int64Index([28], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([29], dtype='int64'),\n",
       " (40.0,\n",
       "  'male'): Int64Index([ 30,  35,  62,  92, 110, 129, 149, 153, 160, 187, 188, 197, 203,\n",
       "             209, 217, 236, 245, 263, 288, 314, 331, 338, 339, 349, 360, 397,\n",
       "             414, 453, 460, 462, 463, 515, 525, 536, 561, 586, 592, 597, 599,\n",
       "             603, 621, 645, 661, 662, 668, 696, 698, 699, 707, 712, 761, 771,\n",
       "             789, 818, 845, 860, 873],\n",
       "            dtype='int64'),\n",
       " (nan, 'female'): Int64Index([31], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([32], dtype='int64'),\n",
       " (60.0,\n",
       "  'male'): Int64Index([33, 54, 170, 252, 280, 326, 438, 456, 545, 555, 570, 587, 625, 684,\n",
       "             694],\n",
       "            dtype='int64'),\n",
       " (nan, 'male'): Int64Index([36], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([19], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([166], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([168], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([176], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([301], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([303], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([304], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([306], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([324], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([330], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([334], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([335], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([347], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([300], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([351], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([358], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([359], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([364], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([367], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([368], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([375], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([384], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([388], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([409], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([354], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([298], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([295], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([284], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([180], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([181], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([185], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([186], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([196], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([198], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([201], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([214], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([223], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([229], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([235], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([240], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([241], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([250], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([256], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([260], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([264], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([270], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([274], dtype='int64'),\n",
       " (60.0, 'female'): Int64Index([275, 366, 483, 829], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([277], dtype='int64'),\n",
       " (nan, 'male'): Int64Index([411], dtype='int64'),\n",
       " (nan, 'female'): Int64Index([888], dtype='int64')}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def get_group(index):\n",
    "    row = titanic.loc[index]\n",
    "    if np.isnan(row.Age): return np.nan\n",
    "    return int(row.Age) - (int(row.Age) % 10)\n",
    "\n",
    "sex_age_group3 = titanic.groupby([get_group, titanic.Sex])\n",
    "\n",
    "sex_age_group3.groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "      Sex   \n",
       "0.0   female    19\n",
       "      male      19\n",
       "10.0  female    34\n",
       "      male       7\n",
       "20.0  female    52\n",
       "      male      25\n",
       "30.0  female    50\n",
       "      male      23\n",
       "40.0  female    22\n",
       "      male      12\n",
       "50.0  female    16\n",
       "      male       4\n",
       "60.0  female     4\n",
       "      male       2\n",
       "70.0  male       0\n",
       "80.0  male       1\n",
       "Name: Survived, dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sex_age_group3.Survived.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Survived</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>Sex</th>\n",
       "      <th>Survived</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">0.0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">female</th>\n",
       "      <th>0</th>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">male</th>\n",
       "      <th>0</th>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">10.0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">female</th>\n",
       "      <th>0</th>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">male</th>\n",
       "      <th>0</th>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">20.0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">female</th>\n",
       "      <th>0</th>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">male</th>\n",
       "      <th>0</th>\n",
       "      <td>123</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">30.0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">female</th>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">male</th>\n",
       "      <th>0</th>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">40.0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">female</th>\n",
       "      <th>0</th>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">male</th>\n",
       "      <th>0</th>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">50.0</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">female</th>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">male</th>\n",
       "      <th>0</th>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">60.0</th>\n",
       "      <th>female</th>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">male</th>\n",
       "      <th>0</th>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>70.0</th>\n",
       "      <th>male</th>\n",
       "      <th>0</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>80.0</th>\n",
       "      <th>male</th>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      Survived\n",
       "     Sex    Survived          \n",
       "0.0  female 0               11\n",
       "            1               19\n",
       "     male   0               13\n",
       "            1               19\n",
       "10.0 female 0               11\n",
       "            1               34\n",
       "     male   0               50\n",
       "            1                7\n",
       "20.0 female 0               20\n",
       "            1               52\n",
       "     male   0              123\n",
       "            1               25\n",
       "30.0 female 0               10\n",
       "            1               50\n",
       "     male   0               84\n",
       "            1               23\n",
       "40.0 female 0               10\n",
       "            1               22\n",
       "     male   0               45\n",
       "            1               12\n",
       "50.0 female 0                2\n",
       "            1               16\n",
       "     male   0               26\n",
       "            1                4\n",
       "60.0 female 1                4\n",
       "     male   0               13\n",
       "            1                2\n",
       "70.0 male   0                6\n",
       "80.0 male   1                1"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "titanic.groupby([get_group, titanic.Sex, titanic.Survived])[['Survived']].count()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
